{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating Your First Model\n",
    "\n",
    "### The Boltzmann Wealth Model "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Important:** \n",
    "- If you are just exploring Mesa and want the fastest way to execute the code we recommend executing this tutorial online in a Colab notebook. [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa/blob/main/docs/tutorials/0_first_model.ipynb) or if you do not have a Google account you can use [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2F0_first_model.ipynb) (This can take 30 seconds to 5 minutes to load)\n",
    "- If you are running locally, please ensure you have the latest Mesa version installed.\n",
    "\n",
    "## Tutorial Description\n",
    "\n",
    "[Mesa](https://github.com/projectmesa/mesa) is a Python framework for [agent-based modeling](https://en.wikipedia.org/wiki/Agent-based_model). This tutorial is the first in a series of introductory tutorials that will assist you in getting started and discover some of the core features of Mesa.  The tutorial starts with the key pieces of a model and then progressively adds functionality. \n",
    "\n",
    "Should anyone find any errors, bugs, have a suggestion, or just are looking for clarification, let us know in our [chat](https://matrix.to/#/#project-mesa:matrix.org)!\n",
    "\n",
    "The premise of this tutorial is to create a starter-level model representing agents exchanging money. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model Description\n",
    "\n",
    "This is a simulated agent-based economy. In an agent-based economy, the behavior of an individual economic agent, such as a consumer or producer, is studied in a market environment.\n",
    "This model is drawn from the field econophysics, specifically a paper prepared by Drăgulescu et al. for additional information on the modeling assumptions used in this model. [Drăgulescu, 2002].\n",
    "\n",
    "The assumption that govern this model are:\n",
    "\n",
    "1. There are some number of agents.\n",
    "2. All agents begin with 1 unit of money.\n",
    "3. At every step of the model, an agent gives 1 unit of money (if they\n",
    "   have it) to some other agent.\n",
    "\n",
    "Even as a starter-level model it yields results that are both interesting and unexpected. \n",
    "\n",
    "Due to its simplicity and intriguing results, we found it to be a good starter model. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tutorial Setup\n",
    "\n",
    "Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.12 or higher is required*.\n",
    "\n",
    "Install Mesa:\n",
    "\n",
    "```bash\n",
    "pip install mesa[rec] \n",
    "```\n",
    "\n",
    "Install Jupyter notebook (optional):\n",
    "\n",
    "```bash\n",
    "pip install jupyter\n",
    "```\n",
    "\n",
    "Install [Seaborn](https://seaborn.pydata.org/) (which is used for data visualization):\n",
    "\n",
    "```bash\n",
    "pip install seaborn\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### IN COLAB? - Run the next cell "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "%pip install --quiet mesa[rec]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building the Sample Model\n",
    "\n",
    "After Mesa is installed a model can be built.\n",
    "\n",
    " This tutorial is written in [Jupyter](http://jupyter.org) to facilitate the explanation portions. \n",
    "\n",
    "Start Jupyter form the command line:\n",
    "\n",
    "```bash\n",
    "jupyter lab\n",
    "```\n",
    "\n",
    "Create a new notebook named `money_model.ipynb` (or whatever you want to call it)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Dependencies\n",
    "This includes importing of dependencies needed for the tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Has multi-dimensional arrays and matrices.\n",
    "# Has a large collection of mathematical functions to operate on these arrays.\n",
    "import numpy as np\n",
    "\n",
    "# Data manipulation and analysis.\n",
    "import pandas as pd\n",
    "\n",
    "# Data visualization tools.\n",
    "import seaborn as sns\n",
    "\n",
    "import mesa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Agent\n",
    "\n",
    "First create the agent. As the tutorial progresses, more functionality will be added to the agent.\n",
    "\n",
    "**Background:** Agents are the individual entities that act in the model. Mesa automatically assigns each agent that is created an integer as a `unique_id.` \n",
    "\n",
    "**Model-specific information:** Agents are the individuals that exchange money, in this case the amount of money an individual agent has is represented as wealth. \n",
    "\n",
    "**Code implementation:** This is done by creating a new class (or object) that extends `mesa.Agent` creating a subclass of  the `Agent` class from mesa. The new class is named `MoneyAgent`. The inherited code of the Mesa agent object can be found in the [mesa repo](https://github.com/projectmesa/mesa/blob/main/mesa/agent.py).\n",
    "\n",
    "The `MoneyAgent` class is created with the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MoneyAgent(mesa.Agent):\n",
    "    \"\"\"An agent with fixed initial wealth.\"\"\"\n",
    "\n",
    "    def __init__(self, model):\n",
    "        # Pass the parameters to the parent class.\n",
    "        super().__init__(model)\n",
    "\n",
    "        # Create the agent's variable and set the initial values.\n",
    "        self.wealth = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Model\n",
    "\n",
    "Next, create the model. This gives us the two basic classes of any Mesa ABM - the agent class (population of agent objects that doing something) and the manager class (a model object that manages the creation, activation, datacollection etc of the agents)\n",
    "\n",
    "**Background:** The model can be visualized as a list containing all the agents. The model creates, holds and manages all the agent objects, specifically in a dictionary. The model activates agents in discrete time steps.\n",
    "\n",
    "**Model-specific information:** When a model is created the number of agents within the model is specified. The model then creates the agents and places them in a set of agents. \n",
    "\n",
    "**Code implementation:** This is done by creating a new class (or object) that extends `mesa.Model` and calls `super().__init__()`, creating a subclass of the `Model` class from mesa. The new class is named `MoneyModel`.  The Mesa code you are using can be found in [model module](https://github.com/projectmesa/mesa/blob/main/mesa/model.py) and the AgentSet in the [agent module](https://github.com/projectmesa/mesa/blob/d7a3834c99a3be809abe2edc8b83610f3d4438ba/mesa/agent.py#L86). A critical point is that you can use the `seed` kwarg (keyword argument) to set a seed which controls the random number generator of the model class allowing for the reproducibility of results. \n",
    "\n",
    "The `MoneyModel` class is created with the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MoneyModel(mesa.Model):\n",
    "    \"\"\"A model with some number of agents.\"\"\"\n",
    "\n",
    "    def __init__(self, n=10, seed=None):\n",
    "        super().__init__(seed=seed)\n",
    "        self.num_agents = n\n",
    "        # Create agents\n",
    "        MoneyAgent.create_agents(model=self, n=n)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Making the Agents `do`\n",
    "\n",
    "With the basics of the Agent class and Model class created we can now activate the agents to `do` things\n",
    "\n",
    "**Background:** Mesa's `do` function calls agent functions to grow your ABM. A step is the smallest unit of time in the model, and is often referred to as a tick. The `do` function and Python functionality can be configured to activate agents in different orders. This can be important as the order in which agents are activated can impact the results of the model [Comer2014]. At each step of the model, one or more of the agents -- usually all of them -- are activated and take their own step, changing internally and/or interacting with one another or the environment.\n",
    "\n",
    "**Model-specific information:** For this section we will randomly reorder the Agent activation order using  `mesa.Agent.shuffle_do` and have the agents `step` function print the agent's unique id that they were assigned during the agent creation process. \n",
    "\n",
    "**Code implementation:** Using standard ABM convention we add a `step` function to the `MoneyModel` class which calls the `mesa.Agent.shuffle_do` function. We then pass into `shuffle_do` the parameter \"step\". This tells mesa to look for and execute the `step` function in our MoneyAgent class.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MoneyAgent(mesa.Agent):\n",
    "    \"\"\"An agent with fixed initial wealth.\"\"\"\n",
    "\n",
    "    def __init__(self, model):\n",
    "        # Pass the parameters to the parent class.\n",
    "        super().__init__(model)\n",
    "\n",
    "        # Create the agent's attribute and set the initial values.\n",
    "        self.wealth = 1\n",
    "\n",
    "    def say_hi(self):\n",
    "        # The agent's step will go here.\n",
    "        # For demonstration purposes we will print the agent's unique_id\n",
    "        print(f\"Hi, I am an agent, you can call me {self.unique_id!s}.\")\n",
    "\n",
    "\n",
    "class MoneyModel(mesa.Model):\n",
    "    \"\"\"A model with some number of agents.\"\"\"\n",
    "\n",
    "    def __init__(self, n=10, seed=None):\n",
    "        super().__init__(seed=seed)\n",
    "        self.num_agents = n\n",
    "\n",
    "        # Create n agents\n",
    "        MoneyAgent.create_agents(model=self, n=n)\n",
    "\n",
    "    def step(self):\n",
    "        \"\"\"Advance the model by one step.\"\"\"\n",
    "        # This function psuedo-randomly reorders the list of agent objects and\n",
    "        # then iterates through calling the function passed in as the parameter\n",
    "        self.agents.shuffle_do(\"say_hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running the Model\n",
    "We now have the pieces of a basic model. The model can be run by creating a model object and calling the step method. The model will run for one step and print the unique_id of each agent. You may run the model for multiple steps by calling the step method multiple times.\n",
    "\n",
    "Create the model object, and run it for one step:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "starter_model = MoneyModel(10)\n",
    "starter_model.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Run this step a few times and see what happens!\n",
    "starter_model.step()\n",
    "# Notice the order of the agents changes each time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Challenge: Change the seed from None to a number like 42 and see the impact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Challenge: Change `shuffle_do` to just `do` and see the impact"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise\n",
    "Modifying the code below to have every agent print out its `wealth` when it is activated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "class MoneyAgent(mesa.Agent):\n",
    "    \"\"\"An agent with fixed initial wealth.\"\"\"\n",
    "\n",
    "    def __init__(self, model):\n",
    "        # Pass the parameters to the parent class.\n",
    "        super().__init__(model)\n",
    "\n",
    "        # Create the agent's variable and set the initial values.\n",
    "        self.wealth = 1\n",
    "\n",
    "    def say_wealth(self):\n",
    "        # The agent's step will go here.\n",
    "        # FIXME: need to print the agent's wealth\n",
    "        print(\"Hi, I am an agent and I am broke!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a model for 12 Agents, and run it for a few steps to see the output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Fixme: Create the model object, and run it"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agents Exchange\n",
    "\n",
    "Returning back to the MoneyAgent the actual exchange process is now going to be created.\n",
    "\n",
    "**Background:** This is where the agent's behavior as it relates to each step or tick of the model is defined.\n",
    "\n",
    "**Model-specific information:** In this case, the agent will check its wealth, and if it has money, give one unit of it away to another random agent.\n",
    "\n",
    "**Code implementation:** The agent's step method is called by `mesa.Agent.shuffle_do(\"exchange\")`during each step of the model. To allow the agent to choose another agent at random, we use the `model.random` random-number generator. This works just like Python's `random` module, but if a fixed seed is  set when the model is instantiated (see earlier challenge), this allows users to replicate a specific model run later. Once we identify this other agent object we increase their wealth by 1 and decrease this agents wealth by one.\n",
    "\n",
    "This updates the step function as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MoneyAgent(mesa.Agent):\n",
    "    \"\"\"An agent with fixed initial wealth.\"\"\"\n",
    "\n",
    "    def __init__(self, model):\n",
    "        # Pass the parameters to the parent class.\n",
    "        super().__init__(model)\n",
    "\n",
    "        # Create the agent's variable and set the initial values.\n",
    "        self.wealth = 1\n",
    "\n",
    "    def exchange(self):\n",
    "        # Verify agent has some wealth\n",
    "        if self.wealth > 0:\n",
    "            other_agent = self.random.choice(self.model.agents)\n",
    "            if other_agent is not None:\n",
    "                other_agent.wealth += 1\n",
    "                self.wealth -= 1\n",
    "\n",
    "\n",
    "class MoneyModel(mesa.Model):\n",
    "    \"\"\"A model with some number of agents.\"\"\"\n",
    "\n",
    "    def __init__(self, n=10):\n",
    "        super().__init__()\n",
    "        self.num_agents = n\n",
    "\n",
    "        # Create agents\n",
    "        MoneyAgent.create_agents(model=self, n=n)\n",
    "\n",
    "    def step(self):\n",
    "        \"\"\"Advance the model by one step.\"\"\"\n",
    "        # This function psuedo-randomly reorders the list of agent objects and\n",
    "        # then iterates through calling the function passed in as the parameter\n",
    "        self.agents.shuffle_do(\"exchange\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running your first model\n",
    "\n",
    "With exchange behavior added, it's time for the first rudimentary run of the model.\n",
    "\n",
    "Now let's create a model with 10 agents, and run it for 30 steps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = MoneyModel(10)  # Tells the model to create 10 agents\n",
    "for _ in range(30):  # Runs the model for 30 steps;\n",
    "    model.step()\n",
    "\n",
    "# Note: An underscore is common convention for a variable that is not used."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we need to get some data out of the model. Specifically, we want to see the distribution of the agent's wealth.\n",
    "\n",
    " We can get the wealth values with list comprehension, and then use seaborn (or another graphics library) to visualize the data in a histogram."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_wealth = [a.wealth for a in model.agents]\n",
    "# Create a histogram with seaborn\n",
    "g = sns.histplot(agent_wealth, discrete=True)\n",
    "g.set(\n",
    "    title=\"Wealth distribution\", xlabel=\"Wealth\", ylabel=\"number of agents\"\n",
    ");  # The semicolon is just to avoid printing the object representation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a better idea of how a model behaves, we can create multiple model objects and see the distribution that emerges from all of them.\n",
    "\n",
    " We can do this with a nested for loop:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_wealth = []\n",
    "# This runs the model 100 times, each model executing 30 steps.\n",
    "for _ in range(100):\n",
    "    # Run the model\n",
    "    model = MoneyModel(10)\n",
    "    for _ in range(30):\n",
    "        model.step()\n",
    "\n",
    "    # Store the results\n",
    "    for agent in model.agents:\n",
    "        all_wealth.append(agent.wealth)\n",
    "\n",
    "# Use seaborn\n",
    "g = sns.histplot(all_wealth, discrete=True)\n",
    "g.set(title=\"Wealth distribution\", xlabel=\"Wealth\", ylabel=\"number of agents\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This runs 100 instantiations of the model, and runs each for 30 steps. \n",
    "\n",
    "Notice that we set the histogram bins to be integers (`discrete=True`), since agents can only have whole numbers of wealth.\n",
    "\n",
    " This distribution looks a lot smoother. By running the model 100 times, we smooth out some of the 'noise' of randomness, and get to the model's overall expected behavior.\n",
    "\n",
    "This outcome might be surprising. Despite the fact that all agents, on average, give and receive one unit of money every step, the model converges to a state where most agents have a small amount of money and a small number have a lot of money."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise\n",
    "Change the above code to see the impact of different model runs, agent populations, and number of steps."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next Steps\n",
    "\n",
    "Check out the [adding space tutorial](https://mesa.readthedocs.io/latest/tutorials/1_adding_space.html) on how to build interactive dashboards for your models."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### More Mesa\n",
    "\n",
    "If you are looking for other Mesa models or tools here are some additional resources. \n",
    "\n",
    "- Example ABMs: Find canonical examples and examples of ABMs demonstrating highlighted features in the [Examples Tab](https://mesa.readthedocs.io/stable/examples.html)\n",
    "- Expanded Examples: Want to integrate Reinforcement Learning or work on the Traveling Salesman Problem? Checkout  [Mesa Examples](https://github.com/projectmesa/mesa-examples/)\n",
    "- Mesa-Geo: If you need an ABM with Geographic Information Systems (GIS) checkout [Mesa-Geo](https://mesa-geo.readthedocs.io/latest/)\n",
    "- Mesa Frames: Have a large complex model that you need to speed up, check out [Mesa Frames](https://github.com/projectmesa/mesa-frames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Happy Modeling!\n",
    "\n",
    "This document is a work in progress.  If you see any errors, exclusions or have any problems please contact [us](https://github.com/projectmesa/mesa/issues)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Comer2014] Comer, Kenneth W. “Who Goes First? An Examination of the Impact of Activation on Outcome Behavior in AgentBased Models.” George Mason University, 2014. http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf\n",
    "\n",
    "[Dragulescu2002] Drăgulescu, Adrian A., and Victor M. Yakovenko. “Statistical Mechanics of Money, Income, and Wealth: A Short Survey.” arXiv Preprint Cond-mat/0211175, 2002. http://arxiv.org/abs/cond-mat/0211175."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
